package com.superm.controller;

import com.alibaba.fastjson.JSON;
import com.superm.entity.*;
import com.superm.service.DepartmentService;
import com.superm.service.EmpService;
import com.superm.service.RoleService;
import com.superm.service.impl.DepartmentServiceImpl;
import com.superm.service.impl.EmpServiceImpl;
import com.superm.service.impl.RoleServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private EmpService empService;

    /**
     * 角色接口
     */
    @Autowired
    private RoleService roleService;
    /**
     * 部门实现类
     */
    @Autowired
    private DepartmentService departmentService;
    /**
     * 跳转到用户管理页面
     * @return
     */
    @RequestMapping("/skipUser")
    public ModelAndView skipUser(){
        ModelAndView mv = new ModelAndView("user/userList");
        //查询所有部门信息
         List<Department> departmentList =   departmentService.selectDepartmentList();
         mv.addObject("departmentList",departmentList);
        return mv;
    }


    /**
     * 模糊查询用户信息
     * @return
     */
    @RequestMapping("/selectLikeUser")
    @ResponseBody  //返回真实数据
    public Map<String,Object> selectLiekUser(EmpJoPo empJoPo,@RequestParam("page") Integer pageNo,@RequestParam("limit")Integer pageSize){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Emp> empList =  empService.selectLikeEmpList(empJoPo,pageNo,pageSize);
        //获取总条数
        Integer count = empService.selectEmpCount(empJoPo);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",empList);
        return map;
    }
    /**
     * 跳转页面至添加用户角色
     * @return
     */
    @RequestMapping("/skipAddRole")
    public ModelAndView skipAddRole(Emp emp){
        ModelAndView mv = new ModelAndView("user/AddRole");
        //获取全部角色
        List<RolePoJo> listRole =roleService.selectAllRoleMap();
        //获取已有角色
        List<String> userHaveRole = roleService.selectAllRole(emp);
        mv.addObject("listRole",JSON.toJSONString(listRole));
        mv.addObject("userHaveRole",userHaveRole);
        mv.addObject("eId",emp.geteId());
        return mv;
    }


    /**
     * 跳转到用户修改页面
     * @return
     */
    @RequestMapping("/skipUpdateUser")
    public ModelAndView skipUpdateUser(Emp emp){
       ModelAndView mv = new ModelAndView("user/userUpdate");
        //查询所有部门信息
        List<Department> departmentList =   departmentService.selectDepartmentList();
        //查询指定用户的信息
        Emp emp1 =   empService.selectIdUser(emp);
        mv.addObject("departmentList",departmentList);
         mv.addObject("emp",emp1);
        return mv;
    }

    /**
     * 跳转到用户添加页面
     * @return
     */
    @RequestMapping("/skipAddUser")
    public ModelAndView skipAddUser(Emp emp){
        ModelAndView mv = new ModelAndView("user/addUser");
        //查询所有部门信息
        List<Department> departmentList =   departmentService.selectDepartmentList();
        mv.addObject("departmentList",departmentList);
        return mv;
    }
    /**
     * 用户信息修改
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public Map<String,Object> updateUser(EmpJoPo empJoPo){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);

        if (empService.updateUser(empJoPo)>0){
            map.put("success",true);
            map.put("msg","修改成功");
        }else {
            map.put("success",false);
            map.put("msg","修改失败");
        }
        return map;
    }

    /**
     * 用户信息添加
     * @return
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public Map<String,Object> addUser(EmpJoPo empJoPo){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);

        if (empService.addUser(empJoPo)>0){
            map.put("success",true);
            map.put("msg","添加成功");
        }else {
            map.put("success",false);
            map.put("msg","添加失败");
        }
        return map;
    }

    /**
     * 删除用户信息
     * @param eId
     * @return
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public Map<String,Object>deleteUser(@RequestParam("eId")Integer eId){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (empService.deleteUser(eId)>0){
            map.put("success",true);
            map.put("msg","删除成功");
        }else{
            map.put("success",false);
            map.put("msg","该用户下有角色信息,无法删除");
        }
        return  map;
    }

    /**
     * 用户状态修改
     * @param eId
     * @param eState
     * @return
     */
    @RequestMapping("/updateOneUser")
    @ResponseBody
    public Map<String,Object> updateOneUser(@RequestParam("eId")Integer eId,@RequestParam("eState")String eState){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);

        if (empService.updateOneUser(eId,eState)>0){
            map.put("success",true);
            map.put("msg","修改成功");
        }else{
            map.put("success",false);
            map.put("msg","修改失败");
        }
        return map;
    }
}
